package com.bosssoft.hr.jobmonitor.config.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/***
 * @Author lys(cherish)
 * @Description Cors跨域配置
 * @Date  2019/2/26
 **/
@Configuration
public class CorsConfig {

    /***
     * @Author lys(cherish)
     * @Description cors配置
     * @Date  2019/2/26
     * @Param []
     * @return org.springframework.web.cors.CorsConfiguration
     **/
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
        corsConfiguration.addAllowedHeader("*"); // 2允许任何头
        corsConfiguration.addAllowedMethod("*"); // 3允许任何方法（post、get等）
        return corsConfiguration;
    }

    /***
     * @Author lys(cherish)
     * @Description  cors过滤器配置
     * @Date  2019/2/26
     * @Param []
     * @return org.springframework.web.filter.CorsFilter
     **/
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4
        return new CorsFilter(source);
    }

}